package com.cnyz.service.impl;

import cn.hutool.db.Page;
import com.cnyz.base.PageResponse;
import com.cnyz.entity.Paper;
import com.cnyz.exception.BaseException;
import com.cnyz.mapper.PaperMapper;
import com.cnyz.service.PaperService;
import com.cnyz.service.QuestionService;
import com.cnyz.utils.UUID;
import com.cnyz.vo.QuestionVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PaperServiceImp implements PaperService {
    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    QuestionService questionService;

    @Override
    public void generatePaperByList(List<Long> questionIds) {
        Long paperId = paperMapper.getUnusedPaperId();
        List<Paper> papers = questionIds.stream()
                .map(questionId -> {
                    Paper paper = new Paper();
                    paper.setPaperId(paperId);
                    paper.setQuestionId(questionId);
                    return paper;
                })
                .collect(Collectors.toList());
        paperMapper.insertPaperByList(papers);
    }

    @Override
    public PageResponse getPaperByPaperId(Integer pageNum,Integer pageSize,Long paperId) {
        List<Long> questionIds = paperMapper.getPaperQuestionsById(paperId);
        if (questionIds == null || questionIds.isEmpty()) {
            throw new BaseException("该试卷为空");
        }
        return questionService.getQuestionPage(pageNum, pageSize, questionIds, null, null, null);//返回
    }
}
